// 获取cookie
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(";");
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) === " ") {
        c = c.substring(1, c.length);
      }
      if (c.indexOf(nameEQ) === 0) {
        return c.substring(nameEQ.length, c.length);
      }
    }
    return null;
  }
// 获取id值
const urlSearchParams = new URLSearchParams(window.location.search);
const id = urlSearchParams.get("id");
const column_id = urlSearchParams.get("column_id");
const names = urlSearchParams.get("name")
document.getElementsByClassName('leftTop')[0].innerHTML = names
function article() {
    axios
    .get("http://127.0.0.1:3000/api/v1/front/contentListOne", {
      params: {
        id : id,
        column_id:column_id
      }
    })
    .then(function (res) {
      if (res.status == 200) {
        if (res.data.code == 0) {
          console.log(res);
          for (let i = 0; i < res.data.data.list.length; i++) {            
            let div = document.createElement("div");
            div.className = "articleShelf";
            div.innerHTML = `<div class="articleTitle">
                        ${res.data.data.list[0].name}
                    </div>
                    <div class="articleDirectory">
                        <div class="source">
                            来源：抖音
                        </div>
                        <div class="author">
                            作者：
                            <span>${res.data.data.list[0].author}</span>
                        </div>
                        <div class="time">
                            发布时间：
                            <span>${(res.data.data.list[0].time).substring(0, 10)}</span>
                        </div>
                        <div class="see">
                            <span>${res.data.data.list[0].see}</span>
                            次浏览
                        </div>
                    </div>
                    <div class="articleCenter">
                        ${res.data.data.list[0].content}
                    </div>
                   `;
            const right = document.getElementsByClassName("right")[0];
            right.appendChild(div);
            let pageOf = document.createElement("div");
            pageOf.className = 'articleBottom';       
            if(res.data.data.goBack.length === 0) {
                pageOf.innerHTML = `<div class="articleNext" data-id="${res.data.data.next[0].id}" onclick="nextPage()">
                            下一篇：
                            <span>${res.data.data.next[0].name}</span>
                        </div>`
            }else if(res.data.data.next.length === 0) {
                pageOf.innerHTML =`<div class="articleGoBack" data-id="${res.data.data.goBack[0].id}" onclick="goBackPage()">
                            上一篇：
                            <span>${res.data.data.goBack[0].name}</span>
                        </div>`
            }else {
                pageOf.innerHTML = `<div class="articleGoBack" data-id="${res.data.data.goBack[0].id}" onclick="goBackPage()">
                            上一篇：
                            <span>${res.data.data.goBack[0].name}</span>
                        </div>
                        <div class="articleNext" data-id="${res.data.data.next[0].id}" onclick="nextPage()">
                            下一篇：
                            <span>${res.data.data.next[0].name}</span>
                        </div>`
            }
           
            right.appendChild(pageOf)
          }
        }
      }
    })
    .catch(function (error) {
      console.log(error);
    });
}
article()

// 进入上一页
function goBackPage() {
    const goBackId = document.getElementsByClassName('articleGoBack')[0].dataset.id
    console.log(goBackId);
    window.location.href = `./acticle.html?column_id=${column_id}&name=${names}&id=${goBackId}`
}

// 进入下一页
function nextPage() {
    let nextId = document.getElementsByClassName('articleNext')[0].dataset.id
    console.log(nextId);
    window.location.href = `./acticle.html?column_id=${column_id}&name=${names}&id=${nextId}`
}

function returnGoBack() {
    let inputValue = document.getElementsByClassName('input')[0].value
    document.cookie = "input=" + inputValue
    if(getCookie('input') !== '') {
        window.location.href = `./home0.html?column_id=${column_id}&name=${names}`
    }
}

